package com.wang.project.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.project.annotation.AuthCheck;
import com.wang.project.common.BaseResponse;
import com.wang.project.common.ErrorCode;
import com.wang.project.common.ResultUtils;
import com.wang.project.constant.UserConstant;
import com.wang.project.exception.BusinessException;
import com.wang.project.model.dto.refreshKey.OrderRefreshKeyDeleteRequest;
import com.wang.project.model.dto.refreshKey.OrderRefreshKeyQueryRequest;
import com.wang.project.model.dto.refreshKey.OrderRefreshKeyUpdateRequest;
import com.wang.project.model.entity.Payment;
import com.wang.project.model.vo.pay.OrderRefreshKeyVO;
import com.wang.project.pay.zfb.ZFBPreorderActionVO;
import com.wang.project.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 针对刷新key相关的支付
 */
@RestController
@RequestMapping("/payment/refreshKey")
@Slf4j
public class PaymentController {
    /**
     * 先与前端约定好
     * 先将支付二维码返回  用户支付完成后 前端进行轮询 对此订单号进行查询订单状态
     */

    @Resource
    private PaymentService paymentService;

    /**
     * 支付刷新key 付钱
     *
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/payToRefreshKey")
    public BaseResponse<ZFBPreorderActionVO> getPayToRefreshKey(HttpServletRequest request, HttpServletResponse response) {
        ZFBPreorderActionVO payToRefreshKey = paymentService.getPayToRefreshKey(request, response);
        return ResultUtils.success(payToRefreshKey);
    }

    // 下面是订单操作

    /**
     * 删除订单
     *
     * @param orderRefreshKeyDeleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUser(@RequestBody OrderRefreshKeyDeleteRequest orderRefreshKeyDeleteRequest) {
        if (orderRefreshKeyDeleteRequest == null || orderRefreshKeyDeleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        return ResultUtils.success(paymentService.removeById(orderRefreshKeyDeleteRequest.getId()));
    }

    /**
     * 更新用户
     *
     * @param orderRefreshKeyUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUser(@RequestBody OrderRefreshKeyUpdateRequest orderRefreshKeyUpdateRequest) {
        if (orderRefreshKeyUpdateRequest == null || orderRefreshKeyUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Payment payment = new Payment();
        BeanUtils.copyProperties(orderRefreshKeyUpdateRequest, payment);
        return ResultUtils.success(paymentService.updateById(payment));
    }

    /**
     * 根据 id 获取用户
     *
     * @param id 订单id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Payment> getUserById(@RequestParam("id") Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(paymentService.getById(id));
    }

    /**
     * 查询所有payment订单信息
     * 只能管理员才能获取订单信息
     *
     * @param orderRefreshKeyQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/queryList")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<OrderRefreshKeyVO>> listPayment(OrderRefreshKeyQueryRequest orderRefreshKeyQueryRequest, HttpServletRequest request) {
        if (orderRefreshKeyQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(paymentService.getPaymentListQuery(orderRefreshKeyQueryRequest, request));
    }

    /**
     * 分页获取payment订单信息
     * 只能管理员才能获取订单信息
     *
     * @param orderRefreshKeyQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<OrderRefreshKeyVO>> listPaymentByPage(OrderRefreshKeyQueryRequest orderRefreshKeyQueryRequest, HttpServletRequest request) {
        Page<OrderRefreshKeyVO> orderRefreshKeyVOPage = paymentService.listPaymentOrderByPage(orderRefreshKeyQueryRequest, request);
        return ResultUtils.success(orderRefreshKeyVOPage);
    }


}
